package com.sys.gradepaysys.controller.event;

import com.alibaba.fastjson.JSON;
import com.sys.gradepaysys.dao.BaseDao;
import com.sys.gradepaysys.entity.Clazz;
import com.sys.gradepaysys.entity.Event;
import com.sys.gradepaysys.entity.ResultData;
import com.sys.gradepaysys.service.EventService;
import com.sys.gradepaysys.service.EventServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "addEventServlet", value = "/addEventServlet")
public class addEventServlet extends HttpServlet {
    private EventService eventService = new EventServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ename = request.getParameter("ename");
        String anEnum = request.getParameter("epay");
        String sdate = request.getParameter("sdate");
        String edate = request.getParameter("edate");
        String grade = request.getParameter("grade");
        String clazz = request.getParameter("clazz");
        List<Clazz> clazzes = BaseDao.selectClazzByGid(grade, clazz);
        Clazz clazz1=new Clazz();
        for (Clazz clazz2 : clazzes) {
            clazz1=clazz2;
        }
        Event event=new Event();
        event.setCid(clazz1.getCid());
        event.seteName(ename);
        event.setePay(Double.valueOf(anEnum));
        event.setsTime(sdate);
        event.seteTime(edate);
        event.seteNum(0);
        boolean b = eventService.addEvent(event);
        String s = "";
        if (b) {
            s = JSON.toJSONString(new ResultData<>(200, "成功添加事件!", null));
        }else {
            s = JSON.toJSONString(new ResultData<>(200, "成功添加失败!", null));
        }
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
